﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace KZ.Express.Setup.Partner
{
    public partial class CustomerGroupView : FormBase
    {
        #region Private Variables

        private object _previousValue;
        private List<DataRow> _changedRow;
        private new CustomerGroupPresenter _presenter;

        #endregion Private Variables

        #region Private Methods

        private void CustomerGroupView_Load(object sender, EventArgs e)
        {
            _changedRow = new List<DataRow>();
            base.LoadData();
        }

        private void dtgCustomerGroup_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.PreviewKeyDown -= Control_PreviewKeyDown;
            e.Control.PreviewKeyDown += Control_PreviewKeyDown;
        }

        private void dtgCustomerGroup_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                if (dtgCustomerGroup.CurrentCell.ColumnIndex == colTemp.Index)
                {
                    if (dtgCustomerGroup.CurrentRow.Index == dtgCustomerGroup.RowCount - 1)
                        base.InsertRow();
                    dtgCustomerGroup.CurrentCell = dtgCustomerGroup[colPartnerGroupName.Index, dtgCustomerGroup.CurrentCell.RowIndex + 1];
                }
                else
                    SendKeys.Send("{TAB}");
            }
        }

        private void dtgCustomerGroup_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (!dtgCustomerGroup.CurrentCell.ReadOnly)
                dtgCustomerGroup.BeginEdit(true);
        }

        private void dtgCustomerGroup_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _previousValue = dtgCustomerGroup[e.ColumnIndex, e.RowIndex].Value;
        }

        private void dtgCustomerGroup_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (_previousValue != dtgCustomerGroup[e.ColumnIndex, e.RowIndex].Value)
                if (base._table.Rows.Count > e.RowIndex && !_changedRow.Contains(base._table.Rows[e.RowIndex]))
                    _changedRow.Add(base._table.Rows[e.RowIndex]);
        }

        private void Control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (dtgCustomerGroup.CurrentCell.ColumnIndex == colPartnerGroupName.Index ||
                    dtgCustomerGroup.CurrentCell.ColumnIndex == colTemp.Index)
                {
                    if (dtgCustomerGroup.CurrentRow.Index == dtgCustomerGroup.RowCount - 1)
                        base.InsertRow();
                }
                else
                    SendKeys.Send("{TAB}");
            }
        }

        #endregion Private Methods

        #region Constructors

        public CustomerGroupView()
        {
            InitializeComponent();
            base.ObjectID = (int)Objects.CustomerGroup;

            base.OrderBy = "PartnerGroupName";
            base.OrderDirection = "ASC";

            base.Action = ActionName.ListWithDeleteSave;
            base.InitializeDataGrid(dtgCustomerGroup);
            base._presenter = _presenter = new CustomerGroupPresenter(this);

            dtgCustomerGroup.ColumnHeaderMouseClick += dtg_ColumnHeaderMouseClick;
        }

        #endregion Constructors

        #region Public Properties

        public override DataTable Table
        {
            get { return base.Table; }
            set
            {
                base.Table = value;
                _changedRow.Clear();
            }
        }

        #endregion Public Properties

        #region Public Methods

        public override void Save()
        {
            try
            {
                dtgCustomerGroup.EndEdit();
                _presenter.Save(_changedRow);
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion Public Methods
    }
}